public class ReverseList {
    public static class ListNode{
        int val;
        ListNode next;

        public ListNode(int val){
            this.val = val;
            this.next = null;
        }
    }
    //方法一：迭代
    public ListNode reverseList(ListNode head){
        ListNode prev = null;
        ListNode curr = head;
        while (curr!=null){
            ListNode next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        return prev;
    }
    //方法二：递归
    public ListNode reverseListUsingRecursion(ListNode head){
        if(head == null || head.next ==null){
            return head;
        }
        ListNode newHead = reverseListUsingRecursion(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
    }
}
